def find_median_sorted_arrays(nums1, nums2):
    """
    查找两个有序数组的中位数：将问题转化为找第k小元素，利用二分缩小范围
    nums1/nums2：两个有序数组
    """
    def get_kth_element(k):
        index1, index2 = 0, 0
        while True:
            # 边界情况
            if index1 == len(nums1):
                return nums2[index2 + k - 1]
            if index2 == len(nums2):
                return nums1[index1 + k - 1]
            if k == 1:
                return min(nums1[index1], nums2[index2])
            # 正常二分
            new_index1 = min(index1 + k//2 - 1, len(nums1)-1)
            new_index2 = min(index2 + k//2 - 1, len(nums2)-1)
            pivot1, pivot2 = nums1[new_index1], nums2[new_index2]
            if pivot1 <= pivot2:
                k -= new_index1 - index1 + 1
                index1 = new_index1 + 1
            else:
                k -= new_index2 - index2 + 1
                index2 = new_index2 + 1

    total_length = len(nums1) + len(nums2)
    if total_length % 2 == 1:
        return get_kth_element((total_length + 1) // 2)
    else:
        return (get_kth_element(total_length // 2) + get_kth_element(total_length // 2 + 1)) / 2

# 测试
nums1 = [1, 3]
nums2 = [2]
print(find_median_sorted_arrays(nums1, nums2))  # 输出：2.0
nums1 = [1, 2]
nums2 = [3, 4]
print(find_median_sorted_arrays(nums1, nums2))  # 输出：2.5